import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:flutterapp/views/decoratedBox.dart';
import 'package:flutterapp/views/flex.dart';
import 'package:flutterapp/views/flow.dart';
import 'package:flutterapp/views/padding.dart';
import 'package:flutterapp/views/scaffold.dart';

import 'components/button.dart';
import 'components/form.dart';
import 'components/image.dart';
import 'components/text.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      //注册路由表
      routes: {
        'new_page':(context) => NewRoute(),
        'new_page1': (context) => EchoRoute(),
        '/':(context) => MyHomePage(title: 'flutter demo home page'),
        'tip2': (context){
          return TipRoute(text:ModalRoute.of(context).settings.arguments);
        },
        'textComponents': (context) => textItem(),
        'buttonComponents': (context) => buttonItem(),
        'imageComponents': (context) => imageItem(),
        'flex': (context) => FlexLayoutTestRoute(),
        'flow': (context) => flowPage(),
        'padding': (context) => PaddingTestRoute(),
        'decorate': (context) => decoratedBox(),
        'scaffold': (context) => ScaffoldRoute(),
      },
      // home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class EchoRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context){
    // 获取路由参数
    var args = ModalRoute.of(context).settings.arguments;
    //。。。
  }
}

class NewRoute extends StatelessWidget {
  @override
  Widget build (BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title:Text('new Route'),
      ),
      body: Center(child: Text('this is new Route'),),
    );
  }
}

class TipRoute extends StatelessWidget {
  TipRoute({
    Key key,
    @required this.text, //接收一个text参数
  }):super(key: key);
  final String text;
  @override 
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text('提示'),
      ),
      body:Padding(
        padding: EdgeInsets.all(18),
        child:Center(
          child:Column(
            children: <Widget>[
              Text(text),
              RaisedButton(
                onPressed: ()=>Navigator.pop(context,'我是返回值'),
                child: Text("返回"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class RouterTestRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context){
    return Center(
      child: RaisedButton(
        onPressed: () async {
          // 打开‘TipRoute’，并等待返回结果
          var result = await Navigator.push(
            context, 
            MaterialPageRoute(
              builder: (context){
                return TipRoute(
                  //路由参数
                  text: '我是提示xxxx',
                );
              },
            ),
          );
          //输出'TipRoute'路由返回结果
          print("路由返回值：$result");
        },
        child:Text('打开提示页'),
      ),
    );
  }
}

class RandomWordsWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final wordPair = new WordPair.random();
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Text(wordPair.toString()),  
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
          child:Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            FlatButton(
              child: Text('open new Route'),
              textColor: Colors.blue,
              onPressed: () {
                // 导航到新路由
                // Navigator.push(context,
                //   MaterialPageRoute(builder: (context){
                //     return NewRoute();
                //   }));
                // 通过命名路由跳转
                Navigator.pushNamed(context, 'new_page');
              },
            ),
            FlatButton(
              child: Text('open TipRoute'),
              textColor: Colors.blue,
              onPressed: () {
                // 导航到新路由
                Navigator.push(context,
                  MaterialPageRoute(builder: (context){
                    return RouterTestRoute();
                  }));
              },
            ),
            FlatButton(
              child: Text('open textComponents'),
              textColor: Colors.blue,
              onPressed: () {
                // 导航到新路由
                Navigator.pushNamed(context, 'textComponents');
              },
            ),
            FlatButton(
              child: Text('open buttonComponents'),
              textColor: Colors.blue,
              onPressed: () {
                // 导航到新路由
                Navigator.pushNamed(context, 'buttonComponents');
              },
            ),
            FlatButton(
              child: Text('open imageComponents'),
              textColor: Colors.blue,
              onPressed: () {
                // 导航到新路由
                Navigator.pushNamed(context, 'imageComponents');
              },
            ),
            FlatButton(
              child: Text('open flexPage'),
              textColor: Colors.blue,
              onPressed: () {
                Navigator.pushNamed(context, 'flex');
              },
            ),
            FlatButton(
              child: Text('open flowPage'),
              textColor: Colors.blue,
              onPressed: () {
                Navigator.pushNamed(context, 'flow');
              },
            ),
            FlatButton(
              child: Text('open paddingPage'),
              textColor: Colors.blue,
              onPressed: () {
                Navigator.pushNamed(context, 'padding');
              },
            ),
            FlatButton(
              child: Text('open decoratedBoxPage'),
              textColor: Colors.blue,
              onPressed: () {
                Navigator.pushNamed(context, 'decorate');
              },
            ),
            FlatButton(
              child: Text('open scaffoldPage'),
              textColor: Colors.blue,
              onPressed: () {
                Navigator.pushNamed(context, 'scaffold');
              },
            ),
            Column(
              // mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                //省略无关代码
                RandomWordsWidget(),
                // textItem()
              ],
            ),
            // LinearProgressIndicator(
            //   backgroundColor: Colors.grey[200],
            //   valueColor: AlwaysStoppedAnimation(Colors.blue),
            // ),
            LinearProgressIndicator(
              backgroundColor: Colors.grey[200],
              valueColor: AlwaysStoppedAnimation(Colors.blue),
              value: .5, 
            ),
            CircularProgressIndicator(
              backgroundColor: Colors.grey[200],
              valueColor: AlwaysStoppedAnimation(Colors.blue),
              value: .5,
            ),
            Container(
              child: SwitchAndCheckBoxTestRoute(),
            )
          ],
        ),
        // floatingActionButton: FloatingActionButton(
        //   onPressed: _incrementCounter,
        //   tooltip: 'Increment',
        //   child: Icon(Icons.add),r
        // ), // This trailing comma makes auto-formatting nicer for build methods.
      )
    );
  }
}
